package com.memory.admin.sell.controller;

import com.memory.common.utils.ResultVoUtil;
import com.memory.common.vo.ResultVo;
import com.memory.component.actionLog.action.UserAction;
import com.memory.component.actionLog.annotation.EntityParam;
import com.memory.modules.business.domain.sell.Logistics;
import com.memory.modules.business.domain.sell.Sell;
import com.memory.modules.business.service.sell.LogisticsService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @PackageName: com.memory.admin.sell.controller
 * @ClassName: LogisticsController
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/10
 */
@Controller
@RequestMapping("/business/logistics")
@RequiredArgsConstructor
public class LogisticsController {

    private final LogisticsService logisticsService;

    @GetMapping("/list")
    public String list(Model model, Logistics logistics){
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<Logistics> example = Example.of(logistics);
        final Page<Logistics> page = logisticsService.findPage(example);
        model.addAttribute("list", page.getContent());
        model.addAttribute("page", page);
        return "/business/logistics/list";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    public String toAdd() {
        return "/business/logistics/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Long id, Model model) {
        final Optional<Logistics> byId = logisticsService.findById(id);
        model.addAttribute("logistics", byId.orElse(new Logistics()));
        return "/business/logistics/add";
    }

    /**
     * 保存添加/修改的数据
     *
     * @param valid 验证对象
     * @param user  实体对象
     */
    @PostMapping("/save")
    @ResponseBody
    @com.memory.component.actionLog.annotation.ActionLog(key = UserAction.USER_SAVE, action = UserAction.class)
    public ResultVo save(@EntityParam Logistics logistics) {
        if (logistics.getId() != null) {
            logisticsService.update(logistics.getId(),logistics);
        } else {
            logisticsService.create(logistics);
        }
        return ResultVoUtil.SAVE_SUCCESS;
    }

}
